package org.alpha.cloud.usercenter.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * @author Alpha Tan
 * @date 2020/3/12 012
 * @comments:
 */
@RestController
@RequestMapping("/hello")
@Slf4j
public class HelloController {
    @GetMapping("/echo")
    public List<String> header(@Autowired HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        List<String> headerList = new ArrayList<>();
        headerList.add("requestUrl=" + url);
        Enumeration<String> headers = request.getHeaderNames();
        while (headers.hasMoreElements()) {
            String key = headers.nextElement();
            headerList.add(key + "=" + request.getHeader(key));
            log.info("key: {}, value={}", key, request.getHeader(key));
        }
        log.info("url: {}", url);
        return headerList;
    }
}
